<?php
/**
 * @file WCParserBehaviorINCLUDE.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-06-05 00:31:35 +0000 (Wed, 05 Jun 2013) $
 *
 * $Id: WCParserBehaviorINCLUDE.php 69 2013-06-05 00:31:35Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0-BETA1/includes/parser/WCParserBehaviorINCLUDE.php $
 */

require_once dirname(__FILE__)."/WCParserBehavior.php";

$WCParserBehaviorMatrix["INCLUDE"]	= "WCParserBehaviorINCLUDE";

class WCParserBehaviorINCLUDE extends WCParserBehavior {
	public function check() {
		if($this->_node) {
			if(count($this->_node->children()) > 1) {
				$this->_errorMsg = "node has more than one children";
				$this->_ok = false;
			} else {
				$this->_ok = true;
				$this->_errorMsg = "Ok";
			}
		} else {
			$this->_errorMsg = "INCLUDE node is not set";
			$this->_ok = false;
		}

		return $this->isOk();
	}
	public function get() {
		$out = false;

		if($this->isOk()) {
			$children	= $this->_node->children();
			$template	= $children[0];

			if($template[0] != "/") {
				$template = $this->_parser->$template;
			}

			$params	= $this->_node->params();
			$path	= WCSanitizer::SystemPath((isset($params["full"]) ? "" : $this->_parser->templatesRoot()).$template);

			if($template && is_readable($path)) {
				$over = array(
						"INCLUDESINGLESTEP" => array()
				);
				if(isset($params["over"])) {
					$overArray = $params["over"];
					if($this->_parser->has($overArray)) {
						$over = $this->_parser->$overArray;
					} else {
						/// @todo check error
					}
				}
					
				$out = "";
				foreach($over as $extraValues) {
					$subRoot = new WCParser();
					$subRoot->massiveAssign($this->_parser->variables());
					$subRoot->templatesRoot($this->_parser->templatesRoot());
					if(isset($params["no-warnings"])) {
						$subRoot->noWarnings();
					} else {
						$subRoot->noWarnings(!$this->_parser->showWarnings());
					}
					if(isset($params["no-summary"])) {
						$subRoot->noSummary();
					} else {
						$subRoot->noSummary(!$this->_parser->showSummary());
					}
					$subRoot->setMaxDepth($this->_parser->maxDepth()-1);
					$subRoot->load($template, isset($params["full"]));

					$subRoot->massiveAssign($extraValues);

					$out .= $subRoot->parse();
					unset($subRoot);
				}

				if(isset($params['padding'])) {
					$padding = str_repeat("\t", $params['padding']);
					$out = (isset($params['nfl'])?'':$padding).str_replace("\n", "\n".$padding, $out);
				}
			} else {
				if(isset($params["!"])) {
					$out = "";
				} else {
					///@todo check error
				}
			}
		}

		return $out;
	}
}

?>